/*
 *
 *  * Copyright (c) 2016. For Intelligent Group.
 *
 */

package com.intelligent.ispc.client.core.view.analysis.spc;

import com.intelligent.ispc.client.utils.GBC;
import com.intelligent.ispc.constant.AppConstant;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
 * Created by Peter on 2016/9/9.
 */
public class SpcExportAllDialog extends JDialog {

    private java.util.List<JCheckBox> checkBoxes;

    /**
     * Constructor
     */
    public SpcExportAllDialog() {
        initComponents();
    }

    /**
     * Initialize component
     */
    public void initComponents() {
        submitBtn = new JButton("Export");
        cancelBtn = new JButton("Cancel");

        this.setLayout(new GridBagLayout());
        checkBoxes = new ArrayList<>();

        ndc = new JCheckBox("Normal Distribution Chart", null, true);
        sc = new JCheckBox("Scatter Chart", null, true);
        runc = new JCheckBox("Run Chart", null, true);
        xbarc = new JCheckBox("X-Bar Chart", null, true);
        sdc = new JCheckBox("SD Chart", null, true);
        rc = new JCheckBox("R Chart", null, true);
        mrc = new JCheckBox("MR Chart", null, true);
        checkBoxes.add(ndc);
        checkBoxes.add(sc);
        checkBoxes.add(runc);
        checkBoxes.add(xbarc);
        checkBoxes.add(sdc);
        checkBoxes.add(rc);
        checkBoxes.add(mrc);

        JPanel infoPanel = new JPanel(new GridBagLayout());
        infoPanel.setBorder(BorderFactory.createTitledBorder("Selected chart"));
        infoPanel.add(ndc, new GBC(0, 0).setFill(GBC.HORIZONTAL));
        infoPanel.add(sc, new GBC(0, 1).setFill(GBC.HORIZONTAL));
        infoPanel.add(runc, new GBC(0, 2).setFill(GBC.HORIZONTAL));
        infoPanel.add(xbarc, new GBC(0, 3).setFill(GBC.HORIZONTAL));
        infoPanel.add(sdc, new GBC(0, 4).setFill(GBC.HORIZONTAL));
        infoPanel.add(rc, new GBC(0, 5).setFill(GBC.HORIZONTAL));
        infoPanel.add(mrc, new GBC(0, 6).setFill(GBC.HORIZONTAL));
        infoPanel.add(new JPanel(), new GBC(0, 7).setFill(GBC.BOTH).setWeight(100, 100));


        JPanel btnPanel = new JPanel();
        btnPanel.setLayout(new GridBagLayout());
        btnPanel.add(new JPanel(), new GBC(0, 0).setFill(GBC.BOTH).setWeight(100, 100));
        btnPanel.add(submitBtn, new GBC(1, 0).setFill(GBC.HORIZONTAL).setInsets(10, 0, 5, 0));
        btnPanel.add(cancelBtn, new GBC(2, 0).setFill(GBC.HORIZONTAL).setInsets(10, 5, 5, 5));

        JLabel title = new JLabel("Select charts that need to be export.");
        this.add(title, new GBC(0, 0).setFill(GBC.HORIZONTAL).setInsets(5));
        this.add(infoPanel, new GBC(0, 1).setFill(GBC.BOTH).setWeight(100, 100).setInsets(5));
        this.add(btnPanel, new GBC(0, 2).setFill(GBC.HORIZONTAL).setInsets(0));

        SpcExportAllDialog d = this;
        cancelBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                d.setVisible(false);
                setCheckBoxSelected();
            }
        });
    }

    public void setCheckBoxSelected() {
        ndc.setSelected(true);
        sc.setSelected(true);
        runc.setSelected(true);
        xbarc.setSelected(true);
        sdc.setSelected(true);
        rc.setSelected(true);
        mrc.setSelected(true);
    }

    public JButton getSubmitBtn() {
        return submitBtn;
    }

    /**
     * Get chart names
     *
     * @return the chart names list
     */
    public java.util.List<String> getChartNames() {
        java.util.List<String> charts = new ArrayList<>();
        for (int i = 0; i < checkBoxes.size(); i++) {
            if (checkBoxes.get(i).isSelected()) {
                charts.add(getChart(i));
            }
        }

        return charts;
    }


    private String getChart(int i) {
        switch (i) {
            case 0:
                return AppConstant.CHART_TYPE_SPC_ND;
            case 1:
                return AppConstant.CHART_TYPE_SPC_SCT;
            case 2:
                return AppConstant.CHART_TYPE_SPC_RUN;
            case 3:
                return AppConstant.CHART_TYPE_SPC_XBAR;
            case 4:
                return AppConstant.CHART_TYPE_SPC_SD;
            case 5:
                return AppConstant.CHART_TYPE_SPC_R;
            case 6:
                return AppConstant.CHART_TYPE_SPC_MR;
            default:
                return null;
        }
    }

    private JButton submitBtn;
    private JButton cancelBtn;

    private JCheckBox ndc;
    private JCheckBox sc;
    private JCheckBox runc;
    private JCheckBox xbarc;
    private JCheckBox sdc;
    private JCheckBox rc;
    private JCheckBox mrc;

}
